var utils = require('../../../../utils/util.js')
var c = require('../../../..//utils/config.js')
Page({
    data: {
        order_no: "",
        orderInfo: [],
        img_url: c.img_url
    },

    onLoad: function (options) {
        var order_no = options.order_no;
        this.setData({
            order_no: order_no
        })
        var token = wx.getStorageSync("userinfo").wap_token;
        this.getOrderInfo();
    },
    getOrderInfo: function () {
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var order_no = this.data.order_no;
        var data = {
            url: "/wsgoods/ordergoods/detail",
            token: token,
            order_no: order_no,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;

                that.setData({
                    orderInfo: data,
                });
            } else {

            }
        });
    },
    Confirmed: function () {
        var order_no = this.data.order_no;
        var that = this;
        wx.showModal({
            title: '提示',
            cancelText: '取消',
            confirmText: '确定',
            content: '是否确认收货？',
            success: function (res) {
                if (res.confirm) {
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var data = {
                        url: "/wsgoods/ordergoods/finish",
                        token: token,
                        order_no: order_no,
                    };
                    utils.request(data).then(function (res) {
                        if (res.code == 0) {
                            utils.sucMsg("收货成功");
                            setTimeout(function () {
                                that.getOrderInfo();
                            }, 1500);
                        } else {

                        }
                    });
                } else if (res.cancel) {

                }
            }
        })
    },
    payOrder: function () {
        var that = this;
        wx.showModal({
            title: '提示',
            cancelText: '取消',
            confirmText: '确定',
            content: '确认支付订单？',
            success: function (res) {
                if (res.confirm) {
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var order_no = that.data.order_no;
                    var data = {
                        url: "/v1/ordergoods/continuePay",
                        token: token,
                        order_no: order_no,
                        openid: wx.getStorageSync("openId"),
                        pay_type: 0,
                    };
                    utils.request(data).then(function (res) {
                        if (res.code == 0) {
                            //直接支付成功!
                            if (res.data.length == 0) {
                                utils.sucMsg("支付成功");
                                setTimeout(function () {
                                    that.getOrderInfo()
                                }, 1500);
                                return false;
                            }
                            //走在线支付
                            wx.requestPayment({
                                'timeStamp': res.data.timeStamp,
                                'nonceStr': res.data.nonceStr,
                                'package': res.data.package,
                                'signType': 'MD5',
                                'paySign': res.data.paySign,
                                'success': function (res) {
                                    utils.sucMsg("支付成功");
                                    setTimeout(function () {
                                        that.getOrderInfo()
                                    }, 1500);
                                },
                                'fail': function (res) {
                                    utils.errMsg("支付失败");
                                    setTimeout(function () {
                                        that.getOrderInfo()
                                    }, 1500);
                                }
                            })
                        } else {
                            utils.errMsg(res.msg);
                        }
                    });
                } else if (res.cancel) {

                }
            }
        })
    },
    /**
     * 申请退款
     */
    applyRefund: function (e) {
        var id = e.currentTarget.dataset.id;
        var that = this;
        wx.showModal({
            title: '提示',
            cancelText: '取消',
            confirmText: '确定',
            content: '确认申请退款该商品？',
            success: function (res) {
                if (res.confirm) {
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var data = {
                        url: "/wsgoods/ordergoods/applyrefund",
                        token: token,
                        order_id: id,
                    };
                    utils.request(data).then(function (res) {
                        if (res.code == 0) {
                          utils.sucMsg("退款申请已提交");
                            setTimeout(function () {
                                that.getOrderInfo();
                            }, 1500);
                        } else {
                            utils.errMsg(res.msg);
                        }
                    });
                } else if (res.cancel) {

                }
            }
        })
    }
})